﻿using DevExpress.XtraEditors;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucPositionAdd : XtraForm
    {
        private dic_position obj;
        private readonly ICommon<dic_position> _exeDic;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucPositionAdd(ICommon<dic_position> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this.obj = new dic_position();
            SetDataIntoUI();
        }

        public xucPositionAdd(dic_position currObj, ICommon<dic_position> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this.obj = currObj;
            SetDataIntoUI();
        }

        private void xucPositionAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (obj == null)
                return;
            switch (_exeDic.myAction)
            {
                case Actions.Update:
                    txtMaChucVu.Text = obj.positioncode;
                    txtTenChucVu.Text = obj.positionname;
                    txtMoTa.Text = obj.description;
                    cbBanQuanTri.Checked = Convert.ToBoolean(obj.ismanager);
                    cbSuDung.Checked = Convert.ToBoolean(obj.active);
                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    obj = new dic_position();
                    txtMaChucVu.Text = _exeDic.NewID("CV");
                    txtTenChucVu.Text = "";
                    txtMoTa.Text = "";
                    cbBanQuanTri.Checked = true;
                    cbSuDung.Checked = true;
                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    obj = new dic_position();
                    txtMaChucVu.Text = _exeDic.NewID("CV");
                    txtTenChucVu.Text = "";
                    txtMoTa.Text = "";
                    cbBanQuanTri.Checked = true;
                    cbSuDung.Checked = true;
                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtMaChucVu.Text == "" || txtTenChucVu.Text == "")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin mã chức vụ hoặc tên chức vụ");
               
                return false;
            }
            obj.positioncode = txtMaChucVu.Text;
            obj.positionname = txtTenChucVu.Text;
            obj.description = txtMoTa.Text;
            obj.active = cbSuDung.Checked;
            obj.ismanager = cbBanQuanTri.Checked;
            return true;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }
    }
}